class Solution(object):
    def updateBoard(self, board, click):
        """
        :type board: List[List[str]]
        :type click: List[int]
        :rtype: List[List[str]]
        """
        x = click[0]
        y = click[1]
        m = len(board)
        n = len(board[0])

        def c(x, y):
            count = 0
            for ux in range(x - 1, x + 2):
                for uy in range(y - 1, y + 2):
                    if 0 <= ux < m and 0 <= uy < n:
                        if board[ux][uy] == 'M':
                            count += 1
            return count

        if board[x][y] == 'M':
            board[x][y] = 'X'
        else:
            if board[x][y] == 'E':
                now = {(x, y)}
                while now:
                    next_set = set()
                    for x, y in now:
                        num = c(x, y)
                        if num:
                            board[x][y] = str(num)
                        else:
                            board[x][y] = 'B'
                            for ux in range(x - 1, x + 2):
                                for uy in range(y - 1, y + 2):
                                    if 0 <= ux < m and 0 <= uy < n:
                                        if board[ux][uy] == 'E':
                                            next_set.add((ux, uy))
                    now = next_set
        return board



data = Solution()
board = [["E", "E", "E", "E", "E"], ["E", "E", "M", "E", "E"], ["E", "E", "E", "E", "E"], ["E", "E", "E", "E", "E"]]
click = [3, 0]
print(data.updateBoard(board, click))
